package com.frame.work.lib;

import com.frame.work.common.base.core.CoreServer;
import com.frame.work.common.constant.CallType;
import com.frame.work.common.constant.DataType;
import com.frame.work.common.util.Frame;
import com.frame.work.common.util.FrameMaker;
import com.frame.work.frame.deal.*;
import com.frame.work.frame.deal.device0fad.AcController0Fad0702Main;
import com.frame.work.frame.deal.device0faa.*;
import com.frame.work.frame.deal.device0fab.*;
import com.frame.work.frame.deal.device0fac.*;
import com.frame.work.frame.deal.device0fb4.*;
import com.frame.work.frame.deal.device0fbe.*;
import com.frame.work.frame.deal.device0fbf.*;
import com.frame.work.frame.deal.device0fc8.*;
import com.frame.work.frame.deal.device0fe6.*;
import com.frame.work.frame.deal.gateway.*;
import com.frame.work.service.TblDeviceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 做数据分类处理
 * '02'写入命令
 * '07'转发命令（透传）
 * '60'入网交互机制
 * '50'网关场景、联动功能
 *
 * @author wjming
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class FrameProcessor {
    private final RedisTemplate redisTemplate;
    private final CoreServer coreServer;
    private final GateWayFrameDeal gateWayFrameDeal;
    private final Device60FrameDeal device60FrameDeal;
    private final Device7002FrameDeal device7002FrameDeal;
    private final Device7001FrameDeal device7001FrameDeal;
    private final Device1018FrameDeal device1018FrameDeal;
    private final Device1015FrameDeal device1015FrameDeal;
    private final Gateway_50_frame_deal gateway_50_frame_deal;
    private final JointControlRespondHandle jointControlRespondHandle;
    private final LinkageControlRespondHandle linkageControlRespondHandle;
    private final InfraredDeviceTimedTaskProcessor infraredDeviceTimedTaskProcessor;
    private final TblDeviceService deviceService;


    public Boolean processFrame(String gatewayMac, String frameString, Object channel, String clientIp) {
        String data = Frame.getData(frameString);
        String dataType = StringUtils.substring(data, 0, 2);
        switch (dataType) {
            case DataType.UP:
                String deviceMac = StringUtils.substring(data, 2, 18);
                String od = StringUtils.substring(data, 18, 22);
                String deviceType = StringUtils.substring(data, 34, 36);
                String productType = StringUtils.substring(data, 36, 38);
                log.info("[processFrame] -> [gatewayMac:{},od:{},数据帧:{}]", gatewayMac, od, frameString);
                switch (od) {

                    case DataType.Gateway.LOGIN:
                        //查看数据后两位是否是00
                        if ("00".equals(StringUtils.substring(data, 22, 24))) {
                            coreServer.writeDataToGateWay(FrameMaker.getWriteGatewayTimeFrame(deviceMac), channel);
                            redisTemplate.opsForValue().set(deviceMac + "_channel", channel, 12, TimeUnit.HOURS);
                            gateWayFrameDeal.loginDeal(deviceMac, data, clientIp);
                        }
                        break;
                    case DataType.Gateway.HEARTBEAT:
                        coreServer.writeDataToGateWay(FrameMaker.getWrite5020HeartResponseFrame(), channel);
                        coreServer.writeDataToGateWay(FrameMaker.getWriteGatewayTimeFrame(deviceMac), channel);
                        redisTemplate.opsForValue().set(deviceMac + "_channel", channel, 12, TimeUnit.HOURS);
                        gateWayFrameDeal.heartDeal(deviceMac, data, clientIp);
                        break;
                    case DataType.ZigbeeDevice.OD_0FAA:
                        if (StringUtils.isEmpty(gatewayMac)) {
                            switch (deviceType + productType) {
                                case "8106":
                                    new ChaKaQuDianMianBan_0FAA8106(gatewayMac, data);
                                    break;
                                case "8107":
                                    new MoHuanChangJingMianBan_0FAA8A07(gatewayMac, data, CallType.TYPE_GET);
                                    break;
                                default:
                                    return false;
                            }
                        }
                        switch (deviceType + productType) {
                            case "0502":
                                new YiLuMianBan_0FAA0502(gatewayMac, data);
                                break;
                            case "0504":
                                new YiLuLianKongMianBan_0FAA0504(gatewayMac, data);
                                break;
                            case "0602":
                                new ErLuMianBan_0FAA0602(gatewayMac, data);
                                break;
                            case "0604":
                                new ErLuLianKongMianBan_0FAA0604(gatewayMac, data);
                                break;
                            case "0702":
                                new SanLuMianBan_0FAA0702(gatewayMac, data);
                                break;
                            case "0704":
                                new SanLuLianKongMianBan_0FAA0704(gatewayMac, data);
                                break;
                            case "0B02":
                                new DuoCaiDeng_0FAA0B02(gatewayMac, data);
                                break;
                            case "0B03":
                                new DengDaiKongZhiQiTwo_0FAA0B03(gatewayMac, data);
                                break;
                            case "8A02":
                                new ChangJingMianBan_0FAA8A02(gatewayMac, data, CallType.TYPE_GET);
                                break;
                            case "8A03":
                                new YeJingChangJingKongZhiQi_0FAA8A03(gatewayMac, data, CallType.TYPE_GET);
                                break;
                            case "8A04":
                                new UXingChangJingKongZhiQi_0FAA8A04(gatewayMac, data);
                                break;
                            case "8A11":
                                new SolVoice0Faa8a11(gatewayMac, data);
                                break;
                            case "0202":
                                new QiangDianKongZhiHe_0FAA0202(gatewayMac, data);
                                break;
                            case "0203":
                                new RuoDianKongZhiHe_0FAA0203(gatewayMac, data);
                                break;
                            case "0210":
                                new TouYingJia_0FAA0210(gatewayMac, data);
                                break;
                            case "0211":
                                new TuiLaKaiChuangQi_0FAA0211(gatewayMac, data);
                                break;
                            case "0212":
                                new PingTuiKaiChuangQi_0FAA0212(gatewayMac, data);
                                break;
                            case "0213":
                                new JiXieShouKongZhiQi_0FAA0213(gatewayMac, data);
                                break;
                            case "0501":
                                new BaLiuChaZuo_0FAA0501(gatewayMac, data);
                                break;
                            case "0511":
                                new YiDongChaZuo_0FAA0511(gatewayMac, data);
                                break;
                            case "0902":
                                new ShengGuangBaoJingQi_0FAA0902(gatewayMac, data);
                                break;
                            case "8102":
                                new KeRanQiTiBaoJingQi_0FAA8102(gatewayMac, data);
                                break;
                            case "8103":
                                new YiYangHuaTanTanCeQi_0FAA8103(gatewayMac, data);
                                break;
                            case "8104":
                                new YanWuChuanGanQi_0FAA8104(gatewayMac, data);
                                break;
                            case "8105":
                                new BaLiuXingRenTiGanYingMianBan_0FAA8105(gatewayMac, data);
                                break;
                            case "8A05":
                                new LiuLuChangJingMianBan_0FAA8A05(gatewayMac, data, CallType.TYPE_GET);
                                break;
                            case "8A06":
                                new SiLuChangJingMianBan_0FAA8A06(gatewayMac, data, CallType.TYPE_GET);
                                break;
                            case "8A07":
                                new MoHuanChangJingMianBan_0FAA8A07(gatewayMac, data, CallType.TYPE_GET);
                                break;
                            case "0804":
                                new SiLuLianKongMianBan_0FAA0804(gatewayMac, data);
                                break;
                            case "8A09":
                                new DuoGongNengKaiGuanMianBan_0FAA8A09(gatewayMac, data);
                                break;
                            case "8A08":
                                new LiuLuShuiYiTie_0FAA8A08(gatewayMac, data, CallType.TYPE_GET);
                                break;
                            case "8A10":
                                new LiuLuChangJingMianBan_0FAA8A10(gatewayMac, data, CallType.TYPE_GET);
                                break;
                            case "8A0A":
                                new ZhuoYueMianBan_0FAA8A0A(gatewayMac, data);
                                break;
                            case "0C02":
                                new LiangYiJia_0FAA0C02(gatewayMac, data);
                                break;
                            case "8106":
                                new ChaKaQuDianMianBan_0FAA8106(gatewayMac, data);
                                break;
                            case "0D02":
                                new ShuangSeWen_0FAA0D02(gatewayMac, data);
                                break;
                            case "0D03":
                                new DanSenWen_0FAA0D03(gatewayMac, data);
                                break;
                            case "0D04":
                                new KaiGuanKongZhiHe_0FAA0D04(gatewayMac, data);
                                break;
                            case "0E02":
                                new JingYinChuangLian_0FAA0E02(gatewayMac, data);
                                break;
                            default:
                                break;
                        }
                        break;
                    case DataType.ZigbeeDevice.OD_0FAB:
                        if (StringUtils.isEmpty(gatewayMac)) {
                            return false;
                        }
                        String dataAfter0FAB = StringUtils.substring(data, 22, 24);
                        switch (deviceType + productType) {
                            case "0202":
                                new BaLuKongZhiQi_0FAB0202(gatewayMac, data);
                                break;
                            case "0602":
                                new SiLuLianKongMianBan_0FAB0602(gatewayMac, data);
                                break;
                            default:
                                break;
                        }
                        break;
                    case DataType.ZigbeeDevice.OD_0FAC:
                        if (StringUtils.isEmpty(gatewayMac)) {
                            return false;
                        }
                        switch (deviceType + productType) {
                            case "0202":
                                new KongTiaoMianBan_0FAC0202(gatewayMac, data);
                                break;
                            case "0302":
                                new DiNuanMianBan_0FAC0302(gatewayMac, data);
                                break;
                            case "0502":
                                ZhiNengKongTiaoMianBan_0FAC0502.dealFrame(gatewayMac, data);
                                break;
                            case "0503":
                                new AcCompanion0Fac0503(gatewayMac, data);
                                break;
                            default:
                                break;
                        }
                        break;
                    case DataType.ZigbeeDevice.OD_0FAD:
                        switch (deviceType + productType) {
                            case "0207":
                                new AcController0Fad0702Main(gatewayMac, data);
                                break;
                            default:
                                break;
                        }

                        break;
                    case DataType.ZigbeeDevice.OD_0FBE:
                        if (StringUtils.isEmpty(gatewayMac)) {
                            switch (deviceType + productType) {
                                case "4002":
                                    new ZhiWenSuo_NbIot_0FBE4002(data);
                                    break;
                                default:
                                    return false;
                            }
                        }
                        switch (deviceType + productType) {
                            case "0102":
                                String dianya = StringUtils.substring(data, data.length() - 16, data.length() - 12);
                                if (dianya.equals("0000")) {
                                    new ZhiNengMenCiChuanGanQi_0FBE0102(gatewayMac, data);
                                } else {
                                    new ZhiNengMenCiMenChuangCi_0FBE0102(gatewayMac, data);
                                }
                                break;
                            // 指纹锁
                            case "0203":
                                new ZhiWenSuo_LiuFu_0FBE0203(gatewayMac, data, CallType.TYPE_UP);
                                break;
                            case "0302":
                                new RanQiChuanGanQi_0FBE0302(gatewayMac, data);
                                break;
                            case "0402":
                                new RenTiHongWaiChuanGanQi_0FBE0402(gatewayMac, data);
                                break;
                            case "0502":
                                new ShuiJinChuanGanQi_0FBE0502(gatewayMac, data);
                                break;
                            case "0702":
                                new YanwuChuanGanQi_0FBE0702(gatewayMac, data);
                                break;
                            case "8102":
                                break;
                            case "8602":
                                new GuaBiShiRenTiHongWai_0FBE8602(gatewayMac, data);
                                break;
                            case "0802":
                                new MoHuanKaiGuan_0FBE0802(gatewayMac, data, CallType.TYPE_GET);
                                break;
                            case "A002":
                                new WenShiDuChuanGanQi_0FBEA002(gatewayMac, data);
                                break;
                            default:
                                break;
                        }
                        break;
                    //德施曼指纹锁
                    case DataType.ZigbeeDevice.OD_0FBF:
                        if (StringUtils.isEmpty(gatewayMac)) {
                            return false;
                        }
                        switch (deviceType + productType) {
                            // 指纹锁
                            case "0202":
                                new ZhiWenSuo_DeShiMan_0FBF0202(gatewayMac, data);
                                break;
                            default:
                                break;
                        }
                        break;
                    case DataType.ZigbeeDevice.OD_0FC8:
                        if (StringUtils.isEmpty(gatewayMac)) {
                            return false;
                        }
                        switch (deviceType + productType) {
                            case "0102":
                                new DanXiangDianBiao_0FC80102(gatewayMac, data);
                                break;
                            case "0202":
                                new JiLiangKongZhiHe_0FC80202(gatewayMac, data);
                                break;
                            case "0402":
                                new JiLiangChaZuo_10A_0FC80402(gatewayMac, frameString);
                                break;
                            case "0403":
                                new JiLiangChaZuo_16A_0FC80403(gatewayMac, frameString);
                                break;
                            default:
                                break;
                        }
                        break;
                    case DataType.ZigbeeDevice.OD_0FB4:
                        if (StringUtils.isEmpty(gatewayMac)) {
                            return false;
                        }
                        switch (deviceType + productType) {
                            case "0102":
                                new WenShiDuChuanGanQi_0FB40102(gatewayMac, data);
                                break;
                            default:
                                return false;
                        }
                        break;
                    case DataType.ZigbeeDevice.OD_0FE6:
                        if (StringUtils.isEmpty(gatewayMac)) {
                            return false;
                        }
                        switch (deviceType + productType) {
                            case "0202":
                                new HongWaiXueXiYi_0FE60202(gatewayMac, data);
                                break;
                            case "0203":
                                new BeiJingYinYue_Fhwise_0FE60203(gatewayMac, data);
                                break;
                            case "0207":
                                new ZhongYangKongTiaoKongZhiQi_0FE60207(gatewayMac, data);
                                break;
                            case "0208":
                                new ZhongYangKongTiaoKongZhiQi_0FE60208(gatewayMac, data);
                                break;
                            case "0209":
                                new JingYinChuangLian_0FE60209(gatewayMac, data, CallType.TYPE_01);
                                break;
                            case "0210":
                                new ZhiNengChuangLian_0FE60210(gatewayMac, data, CallType.TYPE_01);
                                break;
                            case "0211":
                                new PingYiKaiChuangQi_0FE60211(gatewayMac, data, CallType.TYPE_UP);
                                break;
                            case "0206":
                                new QuNuanQi_0FE60206(gatewayMac, data, CallType.TYPE_UP);
                                break;
                            default:
                                break;
                        }
                        break;
                    case DataType.ZigbeeDevice.OD_03EF:
                        device60FrameDeal.frame1007Deal(gatewayMac, data);
                        break;
                    case DataType.ZigbeeDevice.OD_0000:
                        device60FrameDeal.frame0000Deal(gatewayMac, data);
                        break;
                    default:
                        break;
                }
                break;
            //写入成功
            case DataType.WRITE_IN:
                System.out.println("data:" + data);
                String odIndex = StringUtils.substring(data, 18, 22);
                switch (odIndex) {
                    //场景写入
                    case DataType.ZigbeeDevice.OD_1B59:
                        device7001FrameDeal.frame7001Deal(gatewayMac, data);
                        new YeJingChangJingKongZhiQi_0FAA8A03(gatewayMac, data, "set");
                        break;
                    case DataType.ZigbeeDevice.OD_1B5B:
                        device7002FrameDeal.frame7002Deal(gatewayMac, data);
                        break;
                    case DataType.ZigbeeDevice.OD_03EF:
                        device60FrameDeal.frame02setDeal(gatewayMac, data);
                        break;
                    case DataType.ZigbeeDevice.OD_0FAA:
                        device60FrameDeal.frame02setDeal(gatewayMac, data);
                        break;
                    case DataType.ZigbeeDevice.OD_1773:

                        jointControlRespondHandle.processFrame(gatewayMac, data);
                        break;
                    case DataType.ZigbeeDevice.OD_1777:

                        linkageControlRespondHandle.processFrame(gatewayMac, data);

                        System.out.println(StringUtils.substring(data, data.length() - 2, data.length()));
                        break;
                    case DataType.ZigbeeDevice.OD_03FA:
                        device1018FrameDeal.deal(gatewayMac, data);
                        break;
                    case DataType.ZigbeeDevice.OD_03F7:
                        device1015FrameDeal.deal(gatewayMac, data);
                        break;

                    case DataType.ZigbeeDevice.OD_1778:

                        infraredDeviceTimedTaskProcessor.processFrame(gatewayMac, data);
                        break;

                    case DataType.ZigbeeDevice.OD_0FC8:
                        //写入电量（重置电量）
                        deviceMac = data.substring(2, 18);
                        deviceService.resetMeterLog(deviceMac);
                        break;
                    default:
                        break;
                }
                break;
            case DataType.Error_WRITE_IN:
                String odIndex1 = StringUtils.substring(data, 18, 22);
                switch (odIndex1) {
                    case DataType.ZigbeeDevice.OD_03FA:
                        device1018FrameDeal.deal(gatewayMac, data);
                        break;
                    default:
                        break;
                }
                break;
            //透传
            case DataType.FORWARD:
                String od_deviceType_productType = StringUtils.substring(data, 18, 26);
                switch (od_deviceType_productType) {
                    case "0FBE0203":
                        new ZhiWenSuo_LiuFu_0FBE0203(gatewayMac, data, CallType.TYPE_CONTROL);
                        break;
                    case "0FE60209":
                        new JingYinChuangLian_0FE60209(gatewayMac, data, CallType.TYPE_07);
                        break;
                    case "0FE60210":
                        new ZhiNengChuangLian_0FE60210(gatewayMac, data, CallType.TYPE_07);
                        break;
                    case "0FE60203":
                        new BeiJingYinYue_Fhwise_0FE60203(gatewayMac, data, frameString);
                        break;

                    case "0FBE0202":
                        new ZhiWenSuoCongMingWu0Fbe0202(gatewayMac, data, CallType.TYPE_CONTROL);
                        break;

                    case "0FE60211":
                        new PingYiKaiChuangQi_0FE60211(gatewayMac, data, CallType.TYPE_CONTROL);
                        break;

                    case "0FE60206":
                        new QuNuanQi_0FE60206(gatewayMac, data, CallType.TYPE_CONTROL);
                        break;
                    default:
                        break;
                }
                break;
            case DataType.NETWORK_ACCESS:
                device60FrameDeal.frameDeal(gatewayMac, data, channel);
                break;
            case DataType.SCENE:
                gateway_50_frame_deal.frameDeal(gatewayMac, data, channel);
                break;

            default:
                break;
        }
        return true;
    }
}
